const Koa = require('koa');
const app = new Koa(); // 声明实例

const bodyParser = require('koa-bodyparser'); // 引入post解析

const {koaBody} = require('koa-body'); 

const static = require('koa-static'); // 引入对静态文件的访问
const Router = require('koa-router');
const socketIO = require('socket.io');
const http = require('http');
const serve = http.createServer(app.callback());
const io = socketIO(serve);
const cors = require('koa2-cors');



app.use(cors({
    // 允许所有来源，也可以指定特定源，如 'http://example.com'
    origin: '*',
    
    // 允许的请求方法
    methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
    
    // 允许的请求头
    allowedHeaders: ['Content-Type', 'Authorization'],
    
    // 是否允许携带 cookie
    credentials: true,
    
    // 设置预检请求的过期时间（单位：秒）
    maxAge: 86400,
  }));



//   app.use(koaBody({  
//     multipart: true, // 支持文件上传  
//     formidable: {  
//         maxFileSize: 2000*1024*1024,    // 设置上传文件大小最大限制，默认2M  
//     },  
//     jsonLimit: '1mb', // JSON请求体的最大大小  
//     formLimit: '1mb', // 表单数据的最大大小  
//     textLimit: '1mb', // 文本请求体的最大大小  
// })); 




const router = require('./router/router')
const users = require('./router/user')
const words = require('./router/words');
const own = require('./router/own')
const image = require('./router/images');
const edi = require('./router/editor');
const topic = require('./router/topic');
const initializeSocket = require('./router/socket'); 
const history = require('./router/history');
const collect=require('./router/collect');
const publish=require('./router/publish');
const home=require('./router/home');
const match=require('./router/match');
const shequ=require('./router/shequ');
const houguan=require('./router/houguan');
const wode=require('./router/wode');


app.use(bodyParser()); // 使用 bodyParser 解析请求体

app.use(static(__dirname + '/public')); // 提供静态文件服务

app.use(router.routes(),router.allowedMethods());
app.use(users.routes(),users.allowedMethods());
app.use(words.routes(),words.allowedMethods());
app.use(own.routes(),own.allowedMethods());
app.use(image.routes(),image.allowedMethods());
app.use(edi.routes(),edi.allowedMethods());
app.use(topic.routes(),topic.allowedMethods());
app.use(history.routes(),history.allowedMethods());
app.use(collect.routes(),collect.allowedMethods());
app.use(publish.routes(),publish.allowedMethods());
app.use(home.routes(),home.allowedMethods());
app.use(match.routes(),match.allowedMethods());
app.use(shequ.routes(),shequ.allowedMethods());
app.use(houguan.routes(),houguan.allowedMethods());

app.use(wode.routes(),wode.allowedMethods());



  
initializeSocket(serve);
  
serve.listen(3001,()=>{
    console.log('服务器已在3001端口运行')
})

